package com.example.music.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.music.R;

public class IndexAdapter extends RecyclerView.Adapter<IndexAdapter.ViewHolder> {
    private String[] letters;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(String letter);
    }

    public IndexAdapter(String[] letters) {
        this.letters = letters;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_index, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bind(letters[position]);

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(letters[position]);
            }
        });
    }

    @Override
    public int getItemCount() {
        return letters.length;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textViewLetter;

        ViewHolder(View itemView) {
            super(itemView);
            textViewLetter = itemView.findViewById(R.id.textViewLetter);
        }

        void bind(String letter) {
            textViewLetter.setText(letter);
        }
    }
}